<template>
<div>
    <van-nav-bar title="黑马头条 - 登录"/>
      <van-form @submit="onSubmit">
      <van-field
    v-model="formLogin.mobile"
    type="numder"
    required
    name="手机号"
    label="手机号"
    placeholder="请输入手机号"
    :rules="[{ required: true, message: '请填写正确的手机号',trigger:'onChange', pattern : /^1[3-9]\d{9}$/ }]"
  />
  <van-field
    v-model="formLogin.code"
    required
    type="password"
    name="密码"
    label="密码"
    placeholder="请输入密码"
    :rules="[{ required: true, message: '请填写正确密码', trigger:'onChange', pattern:/^\d{6}$/ }]"
  />
  <div style="margin: 16px;">
    <van-button round block type="info"
    :loading="isLoading" :disabled="isLoading" loading-type="spinner" native-type="submit">登录</van-button>
  </div>
</van-form>

</div>
</template>

<script>
import { Notify } from 'vant'
import { mapActions } from 'vuex'
export default {
  name: 'Login',
  data () {
    return {
      isLoading: false,
      formLogin: {
        mobile: '13888888888',
        code: '246810'
      }
    }
  },
  methods: {
    ...mapActions(['asyncLoginAction']),
    async onSubmit (values) {
      // console.log('submit', values)
      this.isLoading = true
      try {
        await this.asyncLoginAction(this.formLogin)
        Notify({ type: 'success', message: '登录成功' })
        this.isLoading = false
        // 点击登录后跳转到layout
        this.$router.replace('/layout')
      } catch (err) {
        // console.log(err)
        Notify({ type: 'warning', message: '登录失败' })
        this.isLoading = false
      }
    }
  }
}
</script>

<style lang="less" scoped>
// .van-nav-bar{
//    background: #007bff;
// }
// /deep/.van-nav-bar__title {
//   color: #fff;
// }
</style>
